package com.ruoyi.quartz.task;

import com.ruoyi.qiushi.domain.deviceData.TblQsBloodPressureRecord;
import com.ruoyi.qiushi.service.deviceData.IQiushiDeviceBloodPressureService;
import com.ruoyi.qiushi.service.deviceData.ITblQsBloodPressureRecordService;
import com.ruoyi.qiushi.service.equipment.IQiushiReportService;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component("deviceDataTask")
public class DeviceDataTask {
    private static final Logger log = LoggerFactory.getLogger(DeviceDataTask.class);


    @Autowired
    IQiushiDeviceBloodPressureService qiushiDeviceBloodPressureService;
    @Autowired
    ITblQsBloodPressureRecordService tblQsBloodPressureRecordService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    private IQiushiReportService reportService;


    public void getBloodPressureData(){
        String startTime = configService.selectConfigByKey("blood_pressure_time");//获取上次获取的时间
        LocalDateTime now = LocalDateTime.now();
        String endTime= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now);
        List<TblQsBloodPressureRecord> data = tblQsBloodPressureRecordService.getData(startTime,endTime);
        log.info("获取到的数据大小:{}",data.size());

        qiushiDeviceBloodPressureService.saveData(data);


        configService.updateConfig("blood_pressure_time",endTime);//更新获取时间
    }

    /**
     * 创建报告
     */
    public void crateBloodPressureReport(){
        reportService.createReport();
    }
}
